//
//  HistogramView.m
//  MonitoringNetwork
//
//  Created by admin on 2017/12/27.
//  Copyright © 2017年 wtmcxlm. All rights reserved.
//

#import "HistogramView.h"
#import "NSString+StringSize.h"
@interface HistogramView()

@end

@implementation HistogramView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        [self setUpUI];
    }
    return self;
}


- (void)setUpUI
{
    
}
- (void)loadHistogramWithArr:(NSArray *)arr
{
    CGSize tempSize = [@"00" sizeWithLabelWidth:100 font:[UIFont systemFontOfSize:NormalFontSize]];
    CGFloat tempHeight = tempSize.height*0.5;
    
    int maxNum = 0;
    int totalNum = 0;
    for (int i=0; i<arr.count; i++) {
        NSDictionary *dic = arr[i];
        totalNum += [dic[@"value"] intValue];
        if (maxNum<[dic[@"value"] intValue]) {
            maxNum = [dic[@"value"] intValue];
        }
    }
    int eachIntValu = maxNum/(4);
    NSString *str  = [NSString stringWithFormat:@"%d",eachIntValu];
    int eachValu =  ceil(maxNum*1.0f/(4)/pow(10,str.length-1))*pow(10,str.length-1);
    
    CGFloat lineVerticalLength = 261-143/3.0f - 188.0f/3;
    CGFloat lineHorizontalLength = kScreenWidth - 57/3.0f - 173.0f/3;
    
    CGFloat verticalLength = lineVerticalLength/(4+1.f);
    CGFloat horizontalLength = lineHorizontalLength/(arr.count);
    
    CGFloat x = 173.0f/3 ;
    CGFloat y = 261-143/3.0f;
    
    NSArray *colorArr = @[ColorOne,ColorTwo];
    
    for (int i=0; i<arr.count; i++) {
        NSDictionary *dic = arr[i];
        int num = [dic[@"value"] intValue];
        CGFloat height = num*1.0f/eachValu*verticalLength;
        UIBezierPath *bezierPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(horizontalLength*(i+0.5)-20*0.5f, lineVerticalLength-height, 20.0f,height) byRoundingCorners:UIRectCornerTopLeft|UIRectCornerTopRight cornerRadii:CGSizeMake(8/3.f, 8/3.f)];
        CAShapeLayer *shaperLayer = [CAShapeLayer new];
        UIColor *color = colorArr[i%2];
        shaperLayer.fillColor = color.CGColor;
        [self.layer addSublayer:shaperLayer];
        shaperLayer.path = bezierPath.CGPath;
        
        UILabel *label = [UILabel LabelWithFont:NormalFontSize andColor:TitleColor andTextAlignment:center andString:[NSString stringWithFormat:@"%@",dic[@"value"]]];
        [self addSubview:label];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(self.mas_left).offset(horizontalLength*(i+0.5));
            make.bottom.mas_equalTo(self.mas_top).offset(lineVerticalLength-height);
        }];
        
    }
    
    
}



@end
